package cn.accjiyun.gomoku.engine;

import cn.accjiyun.gomoku.MainApp;
import cn.accjiyun.gomoku.model.PointValue;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by jiyun on 2016/9/17.
 */
public class GetNextMoves {

    List<PointValue> possibleMoveList;
    private int maxWight;

    public List<PointValue> getPossibleMoveList() {
        return possibleMoveList;
    }

    public GetNextMoves(GomokuAI gomokuAI, int maxWight) {
        this.maxWight = maxWight;
        Evaluate evaluate = new Evaluate(gomokuAI);
        possibleMoveList = evaluate.getPossibleMoveChess();
        this.possibleMoveList = sortNextMove();
    }

    public List<PointValue> sortNextMove() {

        PointValue tempChessPoint;
        for (int i = 0; i < maxWight && i < possibleMoveList.size(); i++) {
            int k = i;
            for (int j = i + 1; j < possibleMoveList.size(); j++) {
                if (possibleMoveList.get(k).getValue() < possibleMoveList.get(j).getValue()) {
                    k = j;
                }
            }
            if (k != i) {
                tempChessPoint = possibleMoveList.get(k);
                possibleMoveList.set(k, possibleMoveList.get(i));
                possibleMoveList.set(i, tempChessPoint);
            }
        }
        List<PointValue> sortPossibleMove = new ArrayList<>();
        for (int i = 0; i < maxWight && i < possibleMoveList.size(); i++) {
            sortPossibleMove.add(possibleMoveList.get(i));
        }
        return sortPossibleMove;
    }

}
